package com.wkh.project.scoring;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wkh.project.aop.ScoringStrategyConfig;
import com.wkh.project.model.dto.question.QuestionContentDto;
import com.wkh.project.model.entity.App;
import com.wkh.project.model.entity.Question;
import com.wkh.project.model.entity.ScoringResult;
import com.wkh.project.model.entity.UserAnswer;
import com.wkh.project.model.vo.QuestionVO;
import com.wkh.project.service.QuestionService;
import com.wkh.project.service.ScoringResultService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author wangkehua
 * 自定义测试类型评分策略
 */
@ScoringStrategyConfig(appType = 1,scoringStrategy = 0)
public class CustomTestScoringStrategy implements ScoringStrategy {

    @Resource
    private QuestionService questionService;
    @Resource
    private ScoringResultService scoringResultService;
    @Override
    public UserAnswer doScore(List<String> choice, App app) throws Exception {
        //1.根据appId获得对应应用的题目
        Question question = questionService.getOne(Wrappers.lambdaQuery(Question.class).eq(Question::getAppId, app.getId()));
        //2.根据appId获得对应评分结果的信息列表
        List<ScoringResult> scoringResultList = scoringResultService.list(Wrappers.lambdaQuery(ScoringResult.class).eq(ScoringResult::getAppId, app.getId()));
        //3.question转换为questionVO
        QuestionVO questionVO = QuestionVO.objToVo(question);
        //4.取出questionContents
        List<QuestionContentDto> questionContents = questionVO.getQuestionContent();
        //5.计算用户选择的答案对应的分数
        //初始化一个Map存储每个选项的计数
        HashMap<String, Integer> optionCount = new HashMap<>();
        //遍历题目列表
        for (QuestionContentDto questionContent : questionContents) {
            //遍历用户选择列表
            for (String answer : choice) {
                for (QuestionContentDto.Option option : questionContent.getOptions()) {
                    //如果答案和选项的key匹配
                    if (answer.equals(option.getKey())){
                        //获取选项中的result
                        String result = option.getResult();
                        //判断result是否在计数Map中
                        if(!optionCount.containsKey(result)){
                            //如果不存在，则添加一个
                            //设置默认值
                            optionCount.put(result,0);
                        }
                        //计数加一
                        optionCount.put(result,optionCount.get(result) + 1);
                    }
                }
            }
        }
        //6.遍历评分结果，比较谁的评分更高
        //初始化最高分对应的评分结果，初始化最高分
        Integer maxScore = 0;
        ScoringResult maxScoringResult = scoringResultList.get(0);
        for (ScoringResult scoringResult : scoringResultList) {
            String resultProp = scoringResult.getResultProp();
            List<String> resultPropList = JSONUtil.toList(resultProp, String.class);
            int score = resultPropList.stream().mapToInt(prop -> optionCount.getOrDefault(prop, 0)).sum();
            if (score > maxScore){
                maxScore = score;
                maxScoringResult = scoringResult;
            }
        }
        //返回对应结果
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(app.getId());
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choice));
        userAnswer.setResultId(maxScoringResult.getId());
        userAnswer.setResultName(maxScoringResult.getResultName());
        userAnswer.setResultDesc(maxScoringResult.getResultDesc());
        userAnswer.setResultPicture(maxScoringResult.getResultPicture());
        return userAnswer;
    }
}
